package DishDemo;

import java.util.LinkedList;
import java.util.Scanner;

public class orderApp {

    //初始化
    static LinkedList<Dish> dish = new LinkedList<>();
    static LinkedList<Dish> orderDish = new LinkedList<>();
    static Scanner sc = new Scanner(System.in);
    static int chose = 0;
    static double sum = 0;
    public static void main(String[] args) {
            initDish();
            while(true){
                showMenu();
                chose = sc.nextInt();
                switch (chose) {
                    case 1 -> {
                        showDishMenu();
                        do {
                            chose = sc.nextInt();
                            if (chose != 0 && chose <= dish.size()) {
                                System.out.println("成功下单： " + dish.get(chose - 1).getName());
                                orderDish.add(new Dish(dish.get(chose - 1).getName(), dish.get(chose - 1).getPrice()));
                            } else {
                                System.out.println("输入有误，请重新输入：");
                            }
                        } while (chose != 0);
                    }
                    case 2 -> {
                        showOrderDishMenu();
                        while (true) {
                            if (sc.nextInt() == 0) {
                                break;
                            } else {
                                System.out.println("请重新输入：");
                            }
                        }
                    }
                    case 3 -> {
                        showFinishOrderMenu();
                        System.out.println("结账完成，谢谢惠顾！（按任意键退出）");
                        sc.next();
                        return;
                    }
                }
            }
    }

    static void initDish() {
        dish.add(new Dish("手撕包菜", 23.0));
        dish.add(new Dish("凉拌皮蛋", 13.0));
        dish.add(new Dish("佛跳墙", 246.0));
        dish.add(new Dish("小炒肉", 25.0));
    }

    static void showMenu(){
        System.out.println("*****菜单******");
        System.out.println("\t1.点菜\t\t");
        System.out.println("\t2.已点\t\t");
        System.out.println("\t3.买单\t\t");
        System.out.println("输入相应数字以进入相应功能：");
    }

    static void showDishMenu(){
        for (int i = 0; i < dish.size(); i++) {
            System.out.println("序号：" + (i+1) + " " + dish.get(i));
        }
        System.out.println("请输入相应数字进行下单(以0结束)：");
    }

    static void showOrderDishMenu(){
        orderDishFor();
        System.out.println("总金额为" + sum);
        System.out.println("输入0返回：");
    }

    static void showFinishOrderMenu(){
        orderDishFor();
        System.out.println("总金额为" + sum);
    }

    static void orderDishFor(){
        sum = 0;
        for (int i = 0; i < orderDish.size(); i++) {
            System.out.println("序号：" + (i+1) + " " + orderDish.get(i));
            sum += orderDish.get(i).getPrice();
        }
    }
}
